package com.zonski.jbots.desktop.client.recolor;

import com.zonski.jbots.desktop.client.Recolorer;

/**
 * Created by IntelliJ IDEA.
 * User: Chris Glover
 * Date: Oct 16, 2003
 * Time: 9:59:56 AM
 * To change this template use Options | File Templates.
 */
public class TwoByteSwapRecolorer implements Recolorer
{
    private int sourceByte;
    private int targetByte1;
    private int targetByte2;

    public TwoByteSwapRecolorer(int sourceByte, int targetByte1, int targetByte2)
    {
        this.sourceByte = sourceByte;
        this.targetByte1 = targetByte1;
        this.targetByte2 = targetByte2;
    }

    public int recolor(int color)
    {
        int result = color;
        int sourceColor = (result >> (this.sourceByte * 8)) & 0xFF;
        int targetColor = (((result >> (this.targetByte1 * 8)) & 0xFF) +
                (result >> (this.targetByte2 * 8) & 0xFF))/2;
        result = result & ~(0xFF << (this.targetByte1 * 8)) & ~(0xFF << (this.targetByte2 * 8));
        result = result | (sourceColor << (this.targetByte1 * 8)) |
                (sourceColor << (this.targetByte2 * 8));
        if(sourceByte != targetByte1 && sourceByte != targetByte2)
        {
            result = result & ~(0xFF << (this.sourceByte * 8));
            result = result | (targetColor << (this.sourceByte * 8));
        }
        return result;

    }
}
